package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.model.IMediaPlayer;
import by.grsu.hdm.devices.MediaPlayer;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.exception.IllegalInternalStateException;
import by.grsu.hdm.model.HomeDeviceStatus;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {

		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();

		MediaCenter media = new MediaCenter("MEDIA#132456");
		media.getUsbPort().connectDevice(new UsbFlashDrive("FLASH-4GB"));

		devices.add(media);
		devices.add(new WashingMachine("WASHING#45"));
		devices.add(new MediaCenter(" JVC "));
		devices.add(new NetworkMonitor("D-link"));

		IMediaPlayer iMediaPlayer = new MediaPlayer();
		//TextReportBuilder textReportBuilder = new TextReportBuilder();
		// turn all devices on..
		for (IHomeDevice device : devices) {

			device.turnOn();

		}
		// Open file for MediaPlayer
		iMediaPlayer.setFile("Lion king");
		// Play media file
		//1 is Play
		//2 is Pause
		//3 is Stop
		iMediaPlayer.setControl(2, "Play");
		
		try {
			
			iMediaPlayer.getControl();
			
		} catch (IllegalInternalStateException e) {

			e.printStackTrace();
		}
		
		
		// check for connected devices
		for (IDevice device : devices) {

			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport) device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();
					System.out.println();
					if (usbDevice != null)
						System.out.println(usbDevice.getDeviceId()
								+ " is connected to " + device.getDeviceId());

				}
			}
		}

		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();

		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable) device);
			}
		}

		TrafficReportData reportData = monitor.getNetworkStatistics();

		TextReportBuilder text = new TextReportBuilder(reportData);
		try {
			text.setFile(iMediaPlayer.getFile(), iMediaPlayer.getControl());
		} catch (IllegalInternalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String report = text.buildReport();

		System.out.println(report);

		// turn all devices off..
		for (IHomeDevice device : devices) {
			device.turnOff();
		}
	}

}
